package com.itheima.web;


import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.Role;
import com.itheima.service.UserService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @outhor
 * @date 2020/2/8 23:04
 */
@RequestMapping("user")
@RestController
public class UserController {

    @Reference
    private UserService userService;

    @RequestMapping("userName")
    public Result getUserName(){
        User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        return new Result(true,"",user.getUsername());
    }
    @RequestMapping("pass")
    public Result changePassword(@RequestBody Map map){
        String oldpass = (String) map.get("oldpass");
        String pass = (String) map.get("pass");
        User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        com.itheima.pojo.User user1 = userService.userByUserName(user.getUsername());
        String password = user1.getPassword();
        if (!encoder.matches(oldpass,password)){
            return new Result(false,"你输入的密码不正确");
        }
        userService.changePassword(encoder.encode(pass),user.getUsername());
        return new  Result(true,"");
    }

    @RequestMapping("findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){
        try {
            PageResult page = userService.findPage(queryPageBean);
            return new Result(true, "查询用户数据成功",page);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询用户数据失败");
        }
    }

    @RequestMapping("findByid")
    public Result findByid(Integer id){
        Map map = null;
        try {
            map = userService.findByid(id);
            return new Result(true,"查询成功",map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询失败",map);
        }
    }

    @RequestMapping("add")
    public Result add(@RequestBody com.itheima.pojo.User user){
        try {
            userService.add(user);
            return new Result(true, "新增用户数据成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "新增用户数据失败");
        }
    }

    @RequestMapping("updata")
    public Result updata(@RequestBody com.itheima.pojo.User user){
        try {
            userService.updata(user);
            return new Result(true, "编辑用户数据成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "编辑用户数据失败");
        }
    }

    @RequestMapping("roleFindAll")
    public Result roleFindAll(){
        Set<Role> roleList = userService.roleFindAll();
        return new Result(true,"",roleList);
    }

    @RequestMapping("delete")
    public Result delete(Integer id){
        try {
            userService.delete(id);
            return new Result(true,"删除用户数据成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true,"删除用户数据失败");
        }
    }
}
